package org.workutil.buildpage.dto;

import java.util.ArrayList;
import java.util.List;

public class PageConditionDto {

	private static final String TAB = "    ";
	private static final String NEW_LINE_TAB = "\r\n    ";
	private static final String END = "\r\n#end\r\n";
	private static final String BUTTON_REMOVE = "<button type=\"button\" class=\"btn btn-default mul-remove\" title=\"删除\">#faIcon(\"trash\")</button>";
	private static final String A_REMOVE = "<a href=\"#\" title=\"删除\" data-opera-type=\"remove\">#faIcon(\"remove\")</a>";
	private static final String SCRIPT_START = "<script>";
	private static final String SCRIPT_END = "\r\n</script>";

	private boolean showOperate = false;
	private boolean showToolbar = false;
	private boolean showState = false;
	private List<Field> fields = new ArrayList<Field>();
	private String tableId = "";
	private String searchPageUrl = "";
	private String addPageUrl = "";
	private String editPageUrl = "";
	private String removeUrl = "";
	private String pk = "";
	private String pageName = "";
	private String formatter = "";
	private String fieldsName = "";

	public String createFormatString() {
		StringBuilder builder = new StringBuilder();

		builder.append("#@btTableFix(\"").append(tableId).append("\")").append(NEW_LINE_TAB);
		builder.append("#include(").append(searchPageUrl).append(")").append(NEW_LINE_TAB);
		builder.append("<a data-url=\"#link(").append(addPageUrl).append(
				")\" href=\"javascript:void(0)\" onclick='showModal(this)'  modal-title=\"新增\" title=\"新增\"  class=\"btn btn-default\" role=\"button\">#faIcon(\"plus\")</a>")
				.append(NEW_LINE_TAB);
		builder.append(BUTTON_REMOVE).append(END);

		builder.append("#@btTableContent(\"").append(searchPageUrl).append("\",{");
		if (!showOperate) {
			builder.append("\"opera\":false,");
		}

		builder.append(
				"\"data-page-list\":\"[10, 20, 50, 100, 200]\",\"data-page-size\":\"10\",\"data-id-field\":\"appId\",\"data-show-export\":\"true\",\"data-cache\":\"false\",\"data-search\":\"false\"})")
				.append(NEW_LINE_TAB);

		builder.append(createFieldString());

		if (showOperate) {
			builder.append("#@btTableOperate()").append(NEW_LINE_TAB);
			builder.append(
					"<a title=\"编辑\" modal-title=\"编辑\" onclick='showModal(this)' href=\"javascript:void(0)\" data-width=\"500\" data-url=\"#link(\"")
					.append(editPageUrl).append("\")\">#faIcon(\"edit\")</a>");
			builder.append(A_REMOVE).append(END);
		}
		return builder.append(createJs()).toString();
	}

	private StringBuilder createFieldString() {
		StringBuilder builder = new StringBuilder();
		if (showState) {
			builder.append(
					"<th data-field=\"state\" data-checkbox=\"true\" data-align=\"left\" data-halign=\"center\"></th>");
		}
		for (int i = 0; i < fields.size(); i++) {
			builder.append("<th data-field=\"").append(fields.get(i).getFieldName())
					.append("\" data-sortable=\"true\" data-align=\"left\" data-halign=\"center\" ")
					.append(formatter == null || "".equals(formatter) ? "" : "data-formatter:" + formatter).append(">")
					.append(fields.get(i).getFieldCNName()).append("</th>").append(NEW_LINE_TAB);
		}
		builder.append(END);
		return builder;

	}

	private StringBuilder createJs() {
		StringBuilder builder = new StringBuilder();
		builder.append(SCRIPT_START).append(NEW_LINE_TAB);
		builder.append("$(function () {").append(NEW_LINE_TAB + TAB);
		builder.append("var _table = $(\"#").append(tableId).append("\");").append(NEW_LINE_TAB + TAB);
		builder.append("var _bttable=$(\"#").append(tableId).append("-table\");").append(NEW_LINE_TAB + TAB);
		builder.append("_table.on(\"click.remove\", \"[data-opera-type=remove]\", function () {")
				.append(NEW_LINE_TAB + TAB + TAB);
		builder.append("var id = $(this).closest(\"tr\").find(\"input[type=checkbox]:first\").val();")
				.append(NEW_LINE_TAB + TAB + TAB);
		builder.append("var ids = [];").append(NEW_LINE_TAB + TAB + TAB);
		builder.append("ids.push(id);").append(NEW_LINE_TAB + TAB + TAB);
		builder.append("layer.confirm(\"确定要删除该信息吗?\", {title: \"删除信息\"}, function () {")
				.append(NEW_LINE_TAB + TAB + TAB);
		builder.append("$.ajax({").append(NEW_LINE_TAB + TAB + TAB + TAB);
		builder.append("url: \"#link(\"").append(removeUrl).append("\")\",").append(NEW_LINE_TAB + TAB + TAB + TAB);
		builder.append("type: \"post\",").append(NEW_LINE_TAB + TAB + TAB + TAB);
		builder.append("data: {" + pk + "s: ids},").append(NEW_LINE_TAB + TAB + TAB + TAB);
		builder.append("dataType: \"json\",").append(NEW_LINE_TAB + TAB + TAB + TAB);
		builder.append("success: function (data) {").append(NEW_LINE_TAB + TAB + TAB + TAB + TAB);
		builder.append("var info = \"系统错误\";").append(NEW_LINE_TAB + TAB + TAB + TAB + TAB);
		builder.append("if (typeof(data.info) != \"undefined\") {").append(NEW_LINE_TAB + TAB + TAB + TAB + TAB + TAB);
		builder.append("info = data.info;").append(NEW_LINE_TAB + TAB + TAB + TAB + TAB).append("}")
				.append(NEW_LINE_TAB + TAB + TAB + TAB + TAB);
		builder.append("if (data.status == \"y\") {").append(NEW_LINE_TAB + TAB + TAB + TAB + TAB + TAB)
				.append("layer.msg(info);").append(NEW_LINE_TAB + TAB + TAB + TAB + TAB).append("} else {")
				.append(NEW_LINE_TAB + TAB + TAB + TAB + TAB + TAB).append("layer.msg(info, {icon: 2})")
				.append(NEW_LINE_TAB + TAB + TAB + TAB + TAB).append("}").append(NEW_LINE_TAB + TAB + TAB + TAB + TAB);
		builder.append("_bttable.bootstrapTable('refresh');").append(NEW_LINE_TAB + TAB + TAB + TAB).append("}")
				.append(NEW_LINE_TAB + TAB + TAB).append("})").append(NEW_LINE_TAB + TAB);
		builder.append("});").append(NEW_LINE_TAB + TAB).append("return false;").append(NEW_LINE_TAB);
		builder.append("}).off(\"click.mulremove\").on(\"click.mulremove\",\".mul-remove\",function(){")
				.append(NEW_LINE_TAB + TAB);
		builder.append("var sels=_bttable.bootstrapTable('getSelections');").append(NEW_LINE_TAB + TAB);
		builder.append("if(sels.length===0){").append(NEW_LINE_TAB + TAB + TAB);
		builder.append("layer.msg(\"请选择\",{icon:2});").append(NEW_LINE_TAB + TAB + TAB);
		builder.append("return false;").append(NEW_LINE_TAB + TAB).append("}").append(NEW_LINE_TAB + TAB);
		builder.append("layer.confirm(\"确定要批量删除所选信息吗?\", {title: \"批量删除信息\"},function() {")
				.append(NEW_LINE_TAB + TAB + TAB);
		builder.append("var ids = [];").append(NEW_LINE_TAB + TAB + TAB);
		builder.append("for (var i = 0, l = sels.length; i < l; i++) {").append(NEW_LINE_TAB + TAB + TAB + TAB);
		builder.append("ids.push(sels[i].dictId);").append(NEW_LINE_TAB + TAB + TAB).append("}")
				.append(NEW_LINE_TAB + TAB);
		builder.append("$.ajax({").append(NEW_LINE_TAB + TAB + TAB).append("url: \"#link(\"").append(removeUrl)
				.append("\")\",").append(NEW_LINE_TAB + TAB + TAB);
		builder.append("type: \"post\",").append(NEW_LINE_TAB + TAB + TAB).append("data: {" + pk + "s:ids},")
				.append(NEW_LINE_TAB + TAB + TAB).append("dataType: \"json\",").append(NEW_LINE_TAB + TAB + TAB)
				.append("success: function (data) {").append(NEW_LINE_TAB + TAB + TAB + TAB);
		builder.append("var info = \"系统错误\";").append(NEW_LINE_TAB + TAB + TAB + TAB)
				.append("if (typeof(data.info) != 'undefined') {").append(NEW_LINE_TAB + TAB + TAB + TAB + TAB);
		builder.append("info = data.info;").append(NEW_LINE_TAB + TAB + TAB + TAB).append("}")
				.append(NEW_LINE_TAB + TAB + TAB + TAB);
		builder.append("if (data && data.status && data.status == \"y\") {")
				.append(NEW_LINE_TAB + TAB + TAB + TAB + TAB);
		builder.append("layer.msg(info);").append(NEW_LINE_TAB + TAB + TAB + TAB).append("} else {")
				.append(NEW_LINE_TAB + TAB + TAB + TAB + TAB);
		builder.append("layer.msg(info, {icon: 2});").append(NEW_LINE_TAB + TAB + TAB + TAB).append("}")
				.append(NEW_LINE_TAB + TAB + TAB + TAB);
		builder.append("_bttable.bootstrapTable('refresh');").append(NEW_LINE_TAB + TAB + TAB).append("}")
				.append(NEW_LINE_TAB + TAB).append("})").append(NEW_LINE_TAB + TAB).append("});")
				.append(NEW_LINE_TAB + TAB).append("return false;").append(NEW_LINE_TAB).append("})")
				.append(NEW_LINE_TAB).append("})").append(SCRIPT_END);
		return builder;
	}

	public boolean isShowOperate() {
		return showOperate;
	}

	public void setShowOperate(boolean showOperate) {
		this.showOperate = showOperate;
	}

	public boolean isShowToolbar() {
		return showToolbar;
	}

	public void setShowToolbar(boolean showToolbar) {
		this.showToolbar = showToolbar;
	}

	public boolean isShowState() {
		return showState;
	}

	public void setShowState(boolean showState) {
		this.showState = showState;
	}

	public String getAddPageUrl() {
		return addPageUrl;
	}

	public void setAddPageUrl(String addPageUrl) {
		this.addPageUrl = addPageUrl;
	}

	public void setFields(List<Field> fields) {
		this.fields = fields;
	}

	public List<Field> getFields() {
		return fields;
	}

	public String getTableId() {
		return tableId;
	}

	public void setTableId(String tableId) {
		this.tableId = tableId;
	}

	public String getSearchPageUrl() {
		return searchPageUrl;
	}

	public void setSearchPageUrl(String searchPageUrl) {
		this.searchPageUrl = searchPageUrl;
	}

	public String getEditPageUrl() {
		return editPageUrl;
	}

	public void setEditPageUrl(String editPageUrl) {
		this.editPageUrl = editPageUrl;
	}

	public String getRemoveUrl() {
		return removeUrl;
	}

	public void setRemoveUrl(String removeUrl) {
		this.removeUrl = removeUrl;
	}

	public String getPk() {
		return pk;
	}

	public void setPk(String pk) {
		this.pk = pk;
	}

	public String getPageName() {
		return pageName;
	}

	public void setPageName(String pageName) {
		this.pageName = pageName;
	}

	public String getFormatter() {
		return formatter;
	}

	public void setFormatter(String formatter) {
		this.formatter = formatter;
	}

	public String getFieldsName() {
		return fieldsName;
	}

	public void setFieldsName(String fieldsName) {
		this.fieldsName = fieldsName;
	}

}
